  library(ggplot2)
  library(stargazer)
  library(effects)
  ### level 3_2 results
  load("final/study3_2.RData")
  
  sink("final/s32_race.tex")
  latex(  tabular(  (Race=race) ~  (Percent("col")+ 1)    ,data=dataPAC       ))
  sink()
  
  sink("final/s32_gender.tex")
  latex(  tabular(  (Gender=gender) ~  (Percent("col")+ 1)    ,data=dataPAC        ))
  sink()
  
  sink("final/s32_edu.tex")
  latex(  tabular(  (Education=education) ~  (Percent("col")+ 1)    ,data=dataPAC        ))
  sink()
  
  sink("final/s32_pid.tex")
  latex(  tabular(  (PartyID=pid) ~  (Percent("col")+ 1)    ,data=dataPAC        ))
  sink()


model<- lm(supportoppose~affpol*condition+affpol+gender+race+age+income+education,dataPAC)

eff<-effect(model,term="affpol*condition",as.table=T,default.levels=100,typical = "median",xlevels=list(affpol=seq(0, 1, by=.01)))
dataeff<-as.data.frame(eff)

d<-ggplot(data=dataeff, aes(x=affpol, y=fit)) + geom_line() + geom_ribbon(aes(ymin=lower, ymax=upper),alpha=0.2,linetype=0)+ xlab("Affective Polarization of the Participant")+ylab("Support for Investigation of the Political Operative") + theme_bw() + scale_colour_manual(values=c("#4d4d4d","#4d4d4d")) 
d<-d + facet_grid(. ~ condition)   +theme(panel.margin = unit(1, "lines")) + theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"),axis.title.x = element_text(vjust=-0.5)) +theme(legend.title=element_blank())+ylim(0,1.05)                       
d
ggsave(filename="final/corruptionAgree.pdf", plot=d,width=9,height=4)

### Results for appendix
modelin <- lm(supportoppose~affpol,subset(dataPAC,condition=='Co-Partisan'))
modelin1 <- lm(supportoppose~affpol+gender+race+age+income+education+pid,subset(dataPAC,condition=='Co-Partisan'))
modelout <- lm(supportoppose~affpol,subset(dataPAC,condition=='Opposing Partisan'))
modelout1 <- lm(supportoppose~affpol+gender+race+age+income+education+pid,subset(dataPAC,condition=='Opposing Partisan'))


stargazer(modelin,modelin1,modelout,modelout1,covariate.labels = c("Affective Polarization","Male","White","Age","Income: 30-59k","Income: 60-79k","Income: 80k+","College+","Some College","Republican","Intercept"),out = "final/three_2_appendix.tex",dep.var.caption = "",keep.stat = c("n","adj.rsq"),title="Affective polarization and support for investigation",dep.var.labels.include = F,model.numbers = F,model.names = F,column.labels = c("In-Party","In-Party","Out-Party","Out-Party"),no.space = T)


stargazer(modelin,modelin1,modelout,modelout1,ci=T)
\#### Broken up by party
modelin1 <- lm(supportoppose~affpol+gender+race+age+income+education,subset(dataPAC,condition=='Co-Partisan' & pid=='Republican'))
modelout1 <- lm(supportoppose~affpol+gender+race+age+income+education,subset(dataPAC,condition=='Opposing Partisan' & pid=='Republican'))
modelin2 <- lm(supportoppose~affpol+gender+race+age+income+education,subset(dataPAC,condition=='Co-Partisan' & pid=='Democrat'))
modelout2 <- lm(supportoppose~affpol+gender+race+age+income+education,subset(dataPAC,condition=='Opposing Partisan' & pid=='Democrat'))

stargazer(modelin1,modelout1,modelin2,modelout1,covariate.labels = c("Affective Polarization","Male","White","Age","Income: 30-59k","Income: 60-79k","Income: 80k+","College+","Some College","Intercept"),out = "final/three_2_appendix_byparty.tex",dep.var.caption = "",keep.stat = c("n","adj.rsq"),title="Affective polarization and support for investigation",dep.var.labels.include = F,model.numbers = F,model.names = F,column.labels = c("In-Party","Out-Party","In-Party","Out-Party"),no.space = T)
